#!/bin/sh
PACKAGE_BASE="/var/packages/nastool/target"
PYTHON_BIN="/var/packages/py3k/target/usr/local/bin/python3"

preinst ()
{
    exit 0
}

install_service(){
  chmod +x "/var/packages/nastool/scripts/"
  # 安装pip
  "${PYTHON_BIN}" "${PACKAGE_BASE}/get-pip.py"
  # pip安装依赖包
  "${PYTHON_BIN}" -m pip install -r "${PACKAGE_BASE}/requirements.txt"
  # 检测Pthon版本
  PYV=$(${PYTHON_BIN} -V 2>&1 | sed 's/.* \([0-9]\).\([0-9]\).*/\1.\2/')
  PYTHON_PACKAGE="/var/packages/py3k/target/usr/local/lib/python${PYV}/site-packages"
  echo "${PACKAGE_BASE}/" > "${PYTHON_PACKAGE}/nas-tools.pth"
  # 配置文件路径
  echo "${cfg_path}" > "${PACKAGE_BASE}/.path"
}

postinst ()
{
  install_service
	exit 0
}

preuninst ()
{
    exit 0
}

postuninst ()
{
    exit 0
}

preupgrade ()
{
    # 保存路径文件
    cp "${PACKAGE_BASE}/.path" "/tmp/.path"
    exit 0
}

postupgrade ()
{
    if [ -f "/tmp/.path" ]; then
      cp -f "/tmp/.path" "${PACKAGE_BASE}/.path"
    fi
    exit 0
}